<?php
require_once 'Pizza.class.php';
require_once 'Order.class.php';
	class Pizzeria {
		private  $name;
		private  $services=array();
		public  static $tableau_pizzas = array();
		private  static $tableau_boissons = array();
		private  static $tableau_menus = array();
		public  static $commande;	
		//tableau associatif renvoy� par connecteur SQL
		public function __construct($SQLResult){
			$this->name=$SQLResult['nom'];
			$count=count($SQLResult['onglets']);
			for($i=0;$i<$count;$i++){
				$this->services[$i]=$SQLResult['onglets'][$i];
			}
			if(isset($_SESSION['user'])){
				$this->commande=new Order($_SESSION['user']);
			}
		}
		public function remplirTableau($tab){
			if(($count = count($tab))>0){
				if($tab[0] instanceof Pizza){
					for($i=0;$i<$count;$i++){
						array_push(self::$tableau_pizzas,$tab[$i]);
					}
				}	
				else if($tab[0] instanceof Boisson){
					for($i=0;$i<$count;$i++){
						$this->tableau_boissons[] = $tab[$i];
					}
				}
				else if($tab[0] instanceof Menus){
					for($i=0;$i<$count;$i++){
						$this->tableau_menus[] = $tab[$i];
					}
				}
			}
		}	
		public function changerMenu($tabMenu){
			$count=count($tabMenu['onglets']);
			for($i=0;$i<$count;$i++){
				$this->services[$i]=$tabMenu['onglets'][$i];
			}
		}
		public static function addCommandePizza($pizzaid){
			self::$commande->addPizza($pizzaid, "Standard", 1);
		}
		public function getMenu(){
			$xml = new DOMDocument;
			$xml->formatOutput = true;
			$onglets=$xml->createElement('onglets');
			$ongletsNode=$xml->appendChild($onglets);
			$count=count($this->services);
			for($i=0;$i<$count;$i++){
				$onglet=$xml->createElement('onglet');
				$onglet->setAttribute('id', $i+1);
				$onglet->setAttribute('name', $this->services[$i]);
				$ongletsNode->appendChild($onglet);
			}
			return $xml->getElementsByTagName('onglets')->item(0);
		}
		public static function getCommande(){
			if(isset($_SESSION['commande']['pizzas'])){
				$count=count($_SESSION['commande']['pizzas']);
				self::$commande=new Order($_SESSION['user']);
				for($i=0;$i<$count;$i++){
					self::addCommandePizza($_SESSION['commande']['pizzas'][$i]);
				}
			}
		}
		public static function getCommandesXML(){
			if(isset($_SESSION['commande']['pizzas'])){
				$count=count($_SESSION['commande']['pizzas']);
				$order=new Order($_SESSION['user']);
				for($i=0;$i<$count;$i++){
					$order->addPizza($_SESSION['commande']['pizzas'][$i],'standard',1);
				}
				return $order->getXML($_SESSION['isadmin']=='yes');
			}
		}
		public function getXML(){
			$isadmin=false;
			if(isset($_SESSION['isadmin'])){
				$isadmin=$_SESSION['isadmin']=='yes';
			}
			//doc XML
			$xml = new DOMDocument('1.0','utf-8');
			$xml->formatOutput = true;
			//pizzeria node
			$pizzeria=$xml->createElement('pizzeria');
			$pizzeriaNode=$xml->appendChild($pizzeria);
			
			// mise en forme node
			$miseEnForme=$xml->createElement('mise_en_forme');
			$miseEnFormeNode=$pizzeriaNode->appendChild($miseEnForme);
			
			//titre
			$titre=$xml->createElement('titre',$this->name);
			$miseEnFormeNode->appendChild($titre);
			
			//onglets
			$miseEnFormeNode->appendChild($xml->importNode($this->getMenu(),true));
			
			//pizzas
			$pizzeriaNode->appendChild($xml->importNode(self::getPizzasXML(),true));
			
			//commandes
			$commandes=$xml->createElement('commandes',' ');
			$commandesNodes=$pizzeriaNode->appendChild($commandes);
			if($isadmin){
				$orderTab=Order::getActive();
				$count=count($orderTab);
				for($i=0;$i<$count;$i++){
					$commandesNodes->appendChild($xml->importNode($orderTab[$i]->getXML($isadmin),true));
				}
			}
			else{
				$this->getCommande();
				if(Pizzeria::getCommandesXML()!=null){
					$commandesList=$xml->importNode(Pizzeria::getCommandesXML(),true);
					$commandesNodes->appendChild($commandesList);
				}
			}
			$connection=$xml->createElement('connection',' ');
			$connectionNode=$pizzeriaNode->appendChild($connection);
			$commandes=new DOMDocument();
			$commandes->appendChild($commandes->importNode($xml->getElementsByTagName('commandes')->item(0),true));
			$_SESSION['pizzeria']=$commandes->saveXML();
			return $xml->saveXML();
		
		}
		public static function getPizzasXML(){
			$xml = new DOMDocument('1.0', 'utf-8');
			$xml->formatOutput=true;
			$pizzas=$xml->createElement('pizzas');
			$pizzasNode=$xml->appendChild($pizzas);
			$count=count(self::$tableau_pizzas);
			for($i=0;$i<$count;$i++){
				$pizzasNode->appendChild($xml->importNode(self::$tableau_pizzas[$i]->getXML(),true));
			}
			return $xml->getElementsByTagName('pizzas')->item(0);
		}
	}
?>